package top.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import top.api.Constants.SystemConstants;
import top.api.common.ResponseResult;
import top.api.domain.dto.RoleAddDto;
import top.api.domain.dto.RoleUpdateDto;
import top.api.domain.dto.RoleUpdateStatusDto;
import top.api.domain.entity.Role;
import top.api.domain.entity.RoleMenu;
import top.api.domain.entity.User;
import top.api.domain.vo.PageVo;
import top.api.domain.vo.RoleInfoVo;
import top.api.domain.vo.RoleVo;
import top.api.service.RoleMenuService;
import top.api.service.RoleService;
import top.api.mapper.RoleMapper;
import org.springframework.stereotype.Service;
import top.api.utils.BeanCopyUtils;
import top.api.utils.SecurityUtils;

import java.util.ArrayList;
import java.util.List;

/**
* @author 27465
* @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
* @createDate 2023-02-01 19:25:18
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKey(User user) {
        // type: 1  管理员返回所有
        if (SecurityUtils.isAdmin()){
            List<String> roleKeyList = new ArrayList<>();
            roleKeyList.add("admin");
            return roleKeyList;
        }

        // 查询对应的角色
        return roleMapper.selectRoleKeyByUserID(user.getId());
    }

    @Override
    public ResponseResult getRoleListPage(Integer pageNum, Integer pageSize, String roleName, String status) {
        // 模糊查询, 状态查询
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(roleName),Role::getRoleName,roleName);
        queryWrapper.eq(StringUtils.hasText(status),Role::getStatus,status);
        // 升序排序
        queryWrapper.orderByAsc(Role::getRoleSort);

        // 分页
        Page<Role> pageInfo = new Page<>(pageNum,pageSize);
        page(pageInfo,queryWrapper);

        // 数据封装
        List<RoleVo> roleVos = BeanCopyUtils.copyBeanList(pageInfo.getRecords(), RoleVo.class);
        PageVo pageVo = new PageVo(roleVos,pageInfo.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult updateStatus(RoleUpdateStatusDto roleUpdateStatusDto) {
        if (! ObjectUtils.isEmpty(roleUpdateStatusDto.getRoleId())){
            LambdaUpdateWrapper<Role> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Role::getId,roleUpdateStatusDto.getRoleId());
            updateWrapper.set(StringUtils.hasText(roleUpdateStatusDto.getStatus()), Role::getStatus,roleUpdateStatusDto.getStatus());

            super.update(updateWrapper);
        }


        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult addRole(RoleAddDto roleAddDto) {
        Role role = BeanCopyUtils.copyBean(roleAddDto, Role.class);
        // 添加角色
        roleMapper.insert(role);

        List<RoleMenu> roleMenus = new ArrayList<>();
        for (String menuId : roleAddDto.getMenuIds()) {
            roleMenus.add(new RoleMenu(role.getId(),Long.valueOf(menuId)));
        }

        // 绑定角色和菜单
        roleMenuService.saveBatch(roleMenus);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRoleInfo(Long id) {
        // 根据id查询角色信息
        Role role = roleMapper.selectById(id);

        RoleInfoVo roleInfoVo = BeanCopyUtils.copyBean(role, RoleInfoVo.class);

        return ResponseResult.okResult(roleInfoVo);
    }

    @Override
    @Transactional
    public ResponseResult updateRole(RoleUpdateDto roleUpdateDto) {
        // 修改角色
        Role role = BeanCopyUtils.copyBean(roleUpdateDto, Role.class);
        roleMapper.updateById(role);
        // 将角色菜单表删除在进行添加
        roleMenuService.removeById(role.getId());

        List<RoleMenu> roleMenus = new ArrayList<>();
        for (String menuId : roleUpdateDto.getMenuIds()) {
            roleMenus.add(new RoleMenu(role.getId(),Long.valueOf(menuId)));
        }

        // 绑定角色和菜单
        roleMenuService.saveBatch(roleMenus);

        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult removeRole(Long id) {
        // 删除角色
        roleMapper.deleteById(id);
        // 删除角色绑定菜单表信息
        roleMenuService.removeById(id);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getListAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.ROLE_STATUS_NORMAL);

        List<Role> roles = roleMapper.selectList(queryWrapper);

        List<RoleInfoVo> roleInfoVo = BeanCopyUtils.copyBeanList(roles,RoleInfoVo.class);

        return ResponseResult.okResult(roleInfoVo);
    }
}




